package com.sanhe.rfmuscle.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class LongClickImageView extends androidx.appcompat.widget.AppCompatImageView {
    private static final String TAG = "LongClickImageView";
    public interface LongClickListener{
        void onActionDown();

        void onActionUp();
    }

    private LongClickListener longClickListener;


    public LongClickImageView(@NonNull Context context) {
        super(context);
        init();
    }

    public LongClickImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LongClickImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setClickable(true);
        setFocusable(true);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "onTouch: ACTION_DOWN");
                if (longClickListener != null) longClickListener.onActionDown();
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "onTouch: ACTION_UP");
                if (longClickListener != null) longClickListener.onActionUp();
                break;
            case MotionEvent.ACTION_MOVE:
            default:
                break;
        }
        return super.onTouchEvent(event);
    }



    public void setLongClickListener(LongClickListener longClickListener){
        this.longClickListener = longClickListener;

    }
}
